<?php
/**
 * Created by Jayli.
 * User: Administrator
 * Date: 2018-03-07
 * Time: 14:46
 */

namespace App\Http\Controllers\Web\Member;

use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class AddressController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'buyer/address'],
        'show' => ['get', 'buyer/address/%d'],
        'store' => ['post', 'buyer/address'],
        'update' => ['put', 'buyer/address/%d'],
        'destroy' => ['delete', 'buyer/address/%d'],
        'update_set_default' => ['put', 'buyer/address/%d/set_default'],
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * 列表页
     * method get
     * url /
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //参数部分
        $params = [
            'type' => 'all',
            'orderBy' => 'updated_at',
            'sortedBy' => 'desc',
        ];

        //请求部分
        $ret = http_web($this->api_url['index'])->send($params);

        return view('web.user.member.address', ['list' => $ret['data']]);
    }

    /**
     * 详情页
     * method get
     * url /$id
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show($id)
    {
        //无详情页
    }

    /**
     * 创建页
     * method get
     * url /create
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('web.user.member.address_edit', ['request_url' => route('address.store')]);
    }

    /**
     * 创建-form提交
     * method post
     * url /
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function store()
    {
        //验证部分
        $data = $this->form();

        //请求部分
        $ret = http_web($this->api_url['store'])->send($data);

        //跳转至列表页，如果有详情页，跳转至详情页
        return $ret;
    }

    /**
     * 更新页
     * method get
     * url /$id/edit
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        //请求详情
        $address = http_web($this->api_url['show'], [$id])->send();

        $address_head=$address['data']['province_name'].$address['data']['city_name'].$address['data']['area_name'];
        $address['data']['address']=str_replace($address_head,'',$address['data']['address']);

        //form old塞值
        old_set($address['data']);

        return view('web.user.member.address_edit', ['request_url' => route('address.update', $id), 'type' => 'put', '']);
    }

    /**
     * 更新-form提交
     * method put
     * url /$id
     * @param $id
     */
    public function update($id)
    {
        //验证部分
        $data = $this->form();

        //请求部分
        $ret = http_web($this->api_url['update'], [$id])->send($data);

        //结果处理部分
        return $ret;
    }

    /**
     * 更新-设置默认值
     * method put
     * url /$id/set_default
     * @param $id
     */
    public function updateSetDefault($id)
    {
        //请求部分
        $ret = http_web($this->api_url['update_set_default'], [$id])->send();

        //结果处理部分
        return $ret;
    }

    /**
     * 删除-ajax提交
     * method delete
     * url /$id
     * @param $id
     */
    public function destroy($id)
    {
        //请求部分
        $ret = http_web($this->api_url['destroy'], [$id])->send();
        return $ret;
    }

    /**
     * 创建、更新公共验证部分
     * @return array
     */
    private function form()
    {
        $this->validate($this->request, [
            'province_code' => 'required|integer',
            'city_code' => 'required|integer',
            'area_code' => 'required|integer',
            'address' => 'required',
            'receiver' => 'required',
            'phone' => 'required|regex:/^1[345789][0-9]{9}$/',
        ], [
            'area_code.required' => '请选择地区',
            'area_code.integer' => '地区选择有误',
            'address.required' => '请填写详细地址',
            'receiver.required' => '请填写收货人',
            'phone.required' => '请填写电话',
            'phone.regex' => '电话格式不正确',
        ]);

        return $this->request->all(['province_code', 'city_code', 'area_code', 'address', 'receiver', 'phone','is_defult']);
    }



    /**
     * 创建-form提交
     * method post
     * url /
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function orderStore()
    {
        //验证部分
        $data = $this->form();

        //请求部分
        $ret = http_web($this->api_url['store'])->send($data);

        //跳转至列表页，如果有详情页，跳转至详情页
        return $ret;
    }
}